package com.mycloud.controller;

import com.mycloud.constant.CookieConstant;
import com.mycloud.constant.RedisContrant;
import com.mycloud.dataobject.UserInfo;
import com.mycloud.enums.ResultEnum;
import com.mycloud.enums.RoleEnum;
import com.mycloud.service.UserService;
import com.mycloud.utils.CookieUtil;
import com.mycloud.utils.ResultVOUtil;
import com.mycloud.vo.ResultVO;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * Created by PC on 2019/1/19.
 */
@RestController
@RequestMapping("/login/")
public class LoginController {

    @Autowired
    private UserService userService;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @GetMapping("buyer")
    public ResultVO buyer(@RequestParam("openId") String openId, HttpServletResponse response) {
        UserInfo userInfo = userService.findByOpenId(openId);
        if (userInfo == null) {
            return ResultVOUtil.error(ResultEnum.LOGIN_FAIL);
        }
        if (!userInfo.getRole().equals(RoleEnum.BUYER.getCode())) {
            return ResultVOUtil.error(ResultEnum.ROLE_ERROR);
        }
        CookieUtil.set(response, CookieConstant.OPENID, openId, CookieConstant.EXPIRE);
        return ResultVOUtil.success();
    }


    @GetMapping("seller")
    public ResultVO seller(@RequestParam("openId") String openId, HttpServletResponse response, HttpServletRequest request) {

        //判断是否登录
        Cookie cookie = CookieUtil.get(request, CookieConstant.TOKEN);
        if(cookie != null &&
                StringUtils.isNotEmpty(stringRedisTemplate.opsForValue().get(String.format(RedisContrant.TOKEN_TEMPLATE, openId)))){
            return ResultVOUtil.success();
        }
        UserInfo userInfo = userService.findByOpenId(openId);
        if (userInfo == null) {
            return ResultVOUtil.error(ResultEnum.LOGIN_FAIL);
        }
        if (!userInfo.getRole().equals(RoleEnum.SELLER.getCode())) {
            return ResultVOUtil.error(ResultEnum.ROLE_ERROR);
        }
        //向redis中写入
        String token = UUID.randomUUID().toString();
        Integer expire = CookieConstant.EXPIRE;
        stringRedisTemplate.opsForValue().set(String.format(RedisContrant.TOKEN_TEMPLATE, token), openId, expire, TimeUnit.SECONDS);
        //向cookie写
        CookieUtil.set(response, CookieConstant.TOKEN, token, CookieConstant.EXPIRE);
        CookieUtil.set(response, CookieConstant.OPENID, openId, CookieConstant.EXPIRE);
        return ResultVOUtil.success();

    }
}
